using System;
using NUnit.Framework;

namespace Drill.Core.Utilities
{
    [TestFixture]
    public class DisposeUtilityTests
    {
        private class DisposableTestObject : IDisposable
        {
            public bool IsDisposed { get; private set; }

            public void Dispose()
            {
                IsDisposed = true;
            }
        }

        private class NonDisposableTestObject
        {
            public bool IsDisposed { get; private set; }

            public void Dispose()
            {
                IsDisposed = true;
            }
        }

        [Test]
        public void DisposeIfPossibleWithNullObjParameter()
        {
            DisposeUtility.DisposeIfPossible(null);
            // Nohting to assert.
            // Should not throw an exception.
        }

        [Test]
        public void DisposeIfPossibleWithDisposableTestObject()
        {
            DisposableTestObject obj = new DisposableTestObject();
            DisposeUtility.DisposeIfPossible(obj);

            Assert.That(obj.IsDisposed, Is.True);
        }

        [Test]
        public void DisposeIfPossibleWithNonDisposableTestObject()
        {
            NonDisposableTestObject obj = new NonDisposableTestObject();
            DisposeUtility.DisposeIfPossible(obj);

            Assert.That(obj.IsDisposed, Is.False);
        }
    }
}